package com.ruoyi.system.wnjk.consumer;

import com.alipay.api.AlipayApiException;
import com.ruoyi.system.pay.domain.dto.OrderQueryDTO;
import com.ruoyi.system.pay.domain.po.OrderQuery;
import com.ruoyi.system.pay.domain.vo.PayOrderVO;
import com.ruoyi.system.pay.service.IPayService;
import com.ruoyi.system.wnjk.config.DirectExchangeConfig;
import com.ruoyi.system.wnjk.contents.Contents;
import com.ruoyi.system.wnjk.domain.po.PhysicalOrder;
import com.ruoyi.system.wnjk.service.IWnjkConsultationOrderService;
import com.ruoyi.system.wnjk.service.impl.WnjkPhysicalExaminationOrderServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @className: DirectQueueConsumer
 * @description: 直连交换机的监听器
 */
@Slf4j
@Component
public class DirectQueueConsumer {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    private IPayService payService;

    @Autowired
    private WnjkPhysicalExaminationOrderServiceImpl physicalExaminationOrderService;

    @Autowired
    private IWnjkConsultationOrderService wnjkConsultationOrderService;

   //监听普通队列
    @RabbitHandler
    @RabbitListener(queues = DirectExchangeConfig.DIRECT_QUEUE)
    public void process(OrderQuery orderQuery){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("DirectReceiver消费者收到消息1  : " + orderQuery + " 接收时间：" + sdf.format(new Date()) + "\n");
        //解决调单
        checkPaymentStatus(orderQuery);
    }

    //监听死信队列
    @RabbitHandler
    @RabbitListener(queues = DirectExchangeConfig.DLX_QUEUE)
    public void process3(OrderQuery orderQuery) {
        System.out.println("死信得列里面的  : " + orderQuery + "\n");
//        rabbitTemplate.convertAndSend(DirectExchangeConfig.DIRECT_EXCHANGE,
//                DirectExchangeConfig.DIRECT_ROUTING_KEY, orderQuery);
        checkPaymentStatus(orderQuery);
    }


    private void checkPaymentStatus(OrderQuery orderQuery) {
        OrderQueryDTO orderQueryDTO = new OrderQueryDTO();
        orderQueryDTO.setOrderNumber(orderQuery.getOrderNumber());
        orderQueryDTO.setSourceType(orderQuery.getSourceType());
        try {
            PayOrderVO payOrderVO = payService.queryOrder(orderQueryDTO);
            if (payOrderVO == null){
                //订单不存在
                //消费消息
                return;
            }
            if (payOrderVO.getStatus().equals(Contents.Pay.TRADE_STATUS_SUCCESS)){
                //支付成功 更新数据库 消费消息
                processData(payOrderVO);
            }else if (payOrderVO.getStatus().equals(Contents.Pay.TRADE_STATUS_WAIT_BUYER_PAY)){
                //系统繁忙 消息再次进入队列
                rabbitTemplate.convertAndSend(
                        DirectExchangeConfig.DIRECT_EXCHANGE,
                        DirectExchangeConfig.DIRECT_ROUTING_KEY,
                        orderQuery);
            }
        } catch (AlipayApiException e) {
            //消费消息失败，重新进入队列
            e.printStackTrace();
            rabbitTemplate.convertAndSend(
                    DirectExchangeConfig.DIRECT_EXCHANGE,
                    DirectExchangeConfig.DIRECT_ROUTING_KEY,
                    orderQuery);
        }
    }

    /**
     *处理查询到的信息
     */
    private void processData(PayOrderVO payOrderVO) {
        //directProducer.sendRequest()
        if (payOrderVO.getSourceType().equals(Contents.Pay.TYPE_GW_SHOP)){
            //购物

        }else if (payOrderVO.getSourceType().equals(Contents.Pay.TYPE_GH_REGISTRATION)){
            //挂号

        }else if (payOrderVO.getSourceType().equals(Contents.Pay.TYPE_WZ_INTERROGATION)){
            //问诊  处理数据

            String orderId = payOrderVO.getOrderNumber();
            String tradeNo = payOrderVO.getTransactionId();

            //更新订单状态
                wnjkConsultationOrderService.updateOrderStatus(orderId, tradeNo);



        }else if (payOrderVO.getSourceType().equals(Contents.Pay.TYPE_TJ_PHYSICAL)){
            //体检
            // 根据传回的订单号，查找当前的订单
            PhysicalOrder order = physicalExaminationOrderService.selectByOutTradeNo(payOrderVO.getOrderNumber());
            order.setStatus("已支付"); // 设置订单状态为查询到的结果
            order.setTradeNo(payOrderVO.getTransactionId());  // 设置流水号
            order.setPaymentTime(payOrderVO.getPaymentTime()); // 设置支付时间
            physicalExaminationOrderService.updateWnjkPhysicalExaminationOrder(order);
        }
    }

}

